﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SqlClient;

namespace Clinica_Frba
{
    public partial class BuscarConsulta : Form
    {
        Conexion sql = Conexion.getInstance();
        Utility util = Utility.getInstance();
        DateTime fechaActual;
        RegAten regAtencion;
        int idMedico;

        public BuscarConsulta()
        {
            InitializeComponent();
        }

        private void BuscarConsulta_Load(object sender, EventArgs e)
        {
            fechaActual = DateTime.ParseExact(util.getAppDate() + " " + util.getAppHora(), "yyyyMMdd HH:mm:ss", CultureInfo.InvariantCulture);
            consultasGrid.CellClick += new DataGridViewCellEventHandler(consultasGrid_CellClick);

            string qSelect, qFrom, qWhere, query;
            SqlDataAdapter adapter;
            DataTable table = new DataTable();

            try
            {
            qSelect = "SELECT Co.Consulta_Numero AS 'Número de Consulta', Co.Id_Paciente AS 'Id Paciente' , Pa.Paciente_Nombre + ' ' + Pa.Paciente_Apellido as 'Paciente' , Co.Consulta_FechaLlegada as 'Fecha de Llegada' ";
            qFrom = "FROM b_sharp.Consultas AS Co " +
                    "JOIN b_sharp.Pacientes AS Pa on Pa.Id_Paciente = Co.Id_Paciente " +
                    "JOIN b_sharp.Turnos AS Tu on Tu.Turno_Numero = Co.Turno_Num ";
            qWhere = "WHERE Co.Id_Medico = " + idMedico + " " +
                        "AND Co.Consulta_FechaAtencion is null " +
                        "AND Co.Consulta_FechaLlegada is not null ";
                        //"AND Tu.Turno_Fecha  between " + fechaActual.Subtract(fechaActual.TimeOfDay) + " and " + fechaActual + " ";
                // ENCONTRAME
            query = qSelect + qFrom + qWhere + ";";

            adapter = sql.execQueryGrid(query);
            adapter.Fill(table);
            consultasGrid.DataSource = table;
            }
            catch
            {
                MessageBox.Show("Error");
                regAtencion.Close();
                this.Close();
            }

            if (consultasGrid.Rows.Count > 0)
            {
                this.consultasGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Seleccionar});
            }
            else
            {
                MessageBox.Show("No tiene consultas disponibles el día de hoy");
                regAtencion.Close();
                this.Close();
            }
            

        }

        public void buscarConsultas(int idMed, RegAten unForm)
        {
            regAtencion = unForm;
            labelIdMedico.Text += Convert.ToString(idMed);
            idMedico = idMed;

            this.ShowDialog();
            
        }

        void consultasGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4)
            {
                int numConsulta = Convert.ToInt32(consultasGrid[0,e.RowIndex].Value);
                int idPac = Convert.ToInt32(consultasGrid[1, e.RowIndex].Value);
                regAtencion.loadConsultas(numConsulta, idPac);
                this.Close();
            }
        }
    }
}
